package 网上的;

import java.util.ArrayList;
import java.util.Scanner;
public class StudentManger {
    public static void main(String[] args) {
        //创建集合对象，存储学生信息
        ArrayList<Student> array = new ArrayList<>();
        while (true) {
            System.out.println("-------------welcome----------");
            System.out.println("1 add");
            System.out.println("2 del");
            System.out.println("3 edit");
            System.out.println("4 list");
            System.out.println("5 quit");
            System.out.println("please chose:");
            Scanner sc = new Scanner(System.in);
            String line = sc.nextLine();
            switch (line) {
                case "1":
                    //System.out.println("add");
                    addStudent(array);
                    break;
                case "2":
                    deleteStudent(array);
                    break;
                case "3":
                    updateStudent(array);
                    break;
                case "4":
                    findAllStudent(array);
                    break;
                case "5":
                    System.out.println("thanks");
                    System.exit(0); //java虚拟机的退出
            }
        }
    }
    //添加学生信息
    public static void addStudent(ArrayList<Student> array) {
        //对信息的输入
        Scanner sc = new Scanner(System.in);
        System.out.println("学号");
        String sid = sc.nextLine();
        System.out.println("姓名");
        String name = sc.nextLine();
        System.out.println("年龄");
        String age = sc.nextLine();
        System.out.println("地址");
        String address = sc.nextLine();
        //创建、赋值
        Student s = new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);
        array.add(s);
        System.out.println("添加成功");
    }
    //查看学生信息
    public static void findAllStudent(ArrayList<Student> array) {
        System.out.println("学号\t姓名\t年龄\t居住地\t");
        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            System.out.println(s.getSid() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress());
        }
    }
    //删除学生
    public static void deleteStudent(ArrayList<Student> array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入你要删除的学号");
        String sid = sc.nextLine();
        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            if (s.getSid().equals(sid)) {
                array.remove(i);
                break;
            }
        }
        System.out.println("删除学生成功");
    }
    //修改学生
    public static void updateStudent(ArrayList<Student> array){
        Scanner sc = new Scanner(System.in);
        System.out.println("输入你要修改的学号");
        String sid = sc.nextLine();
        System.out.println("姓名");
        String name = sc.nextLine();
        System.out.println("年龄");
        String age = sc.nextLine();
        System.out.println("地址");
        String address = sc.nextLine();
        //创建、赋值
        Student s = new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);
        for(int i=0;i<array.size();i++){
            Student stu=array.get(i);
            if(stu.getSid().equals(sid)){
                array.set(i,s);
                break;
            }
        }
        System.out.println("修改成功");
    }
}
